const {getCachedAccessToken} = require("./coze");
const {CozeAPI, COZE_CN_BASE_URL} = require("@coze/api");
module.exports= {
    /**
     * 插入VIP信息
     */
    async insertVipInfo(that,{user_id,vip_type,vip_expire_time}){
        const { ctx, app } = that;
        if (user_id){
            const userInfo = await app.mysql.get('users', {
                id: user_id,
            });
            if (!userInfo) {
                return {
                    success: false,
                    message: '找不到该用户'
                };
            }
            // const result = await app.mysql.insert('users', {
            //     id: user_id,
            //     vip_type: vip_type,
            //     vip_expire_time: vip_expire_time
            // })
        }
    },

    /**
     * 根据bot_id获取智能体token，若token过期，则更新
     */
    async getAgentToken(that,{bot_id,conversation_id,user_id }){
        const { ctx, app } = that;
        let agentInfo = {};
        let userInfo = {};
        let agent_access_token = '';
        if (bot_id){
            agentInfo = await app.mysql.get('agents', {
                bot_id: bot_id,
            });
            if (!agentInfo) {
                return {
                    success: false,
                    message: '找不到该智能体'
                };
            }
            agent_access_token = agentInfo.access_token;
        }else if (conversation_id){
            // 根据conversation_id获取智能体token
            const conversationInfo = await app.mysql.get('conversations', {
                conversation_id: conversation_id,
            });
            agentInfo = await app.mysql.get('agents', {
                bot_id: conversationInfo.bot_id,
            });
            if (!agentInfo) {
                return {
                    success: false,
                    message: '找不到该智能体'
                };
            }
            agent_access_token = agentInfo.access_token;
        }else if (user_id){
            // 根据user_id获取智能体token
             userInfo = await app.mysql.get('users', {
                id: user_id,
            });
            agent_access_token = userInfo.coze_access_token;
        }else{
            return {
                success: false,
                message: '参数错误'
            };
        }
        const cozeApi = new CozeAPI({
            token: agent_access_token,
            baseURL: COZE_CN_BASE_URL
        })
        console.log('更新accessToken',agent_access_token)
         cozeApi.bots.listNew().then(async (res) => {

         }).catch(async (res) => {
             if (res.code === 4100) {
                 // 检查是否过期
                 agent_access_token = await getCachedAccessToken();
                 // update agentInfo信息
                 if(agentInfo.id){
                     await app.mysql.update('agents', {id:agentInfo.id, access_token: agent_access_token})
                 }
                 if(userInfo.id){
                     await app.mysql.update('users', {id:userInfo.id, coze_access_token: agent_access_token})
                 }
                 return {
                     success: true,
                     data: agent_access_token
                 };
             }
         });

        return {
            success: true,
            data: agent_access_token
        };
    }

}